package com.zzh.takeoutapp.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.AddressAdapter;
import com.zzh.takeoutapp.adapter.CommentAdapter;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.model.AddressBean;
import com.zzh.takeoutapp.model.EvaluateBean;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.GsonResolve;
import com.zzh.takeoutapp.utils.ImageManager;
import com.zzh.takeoutapp.utils.LoadingUtil;
import com.zzh.takeoutapp.utils.ToolBarHelper;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/30.
 */

public class CommentActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private ImageView me_headportrait;
    private CommentAdapter evaluateAdapter;
    private int CURPAGE = 1;//加载页码
    private final String COMMENT_ACTIVITY = "CommentActivity";
    private View view;
    private LoadingUtil loadingUtil;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_comment);
        ButterKnife.bind(this);
    }

    @Override
    public void initComponent() {
        ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "我的评论");
        view = LayoutInflater.from(this).inflate(R.layout.mine_head_layout, null);
        me_headportrait = view.findViewById(R.id.me_headportrait);
        TextView tv_user_name = view.findViewById(R.id.tv_user_name);
        tv_user_name.setText(getIntent().getStringExtra("nickname"));
        ImageManager.loadUrlImage(this, getIntent().getStringExtra("face"), me_headportrait);
        loadingUtil = new LoadingUtil(this);
        loadingUtil.startShowLoading();
        ServiceUrl.CommentService(this, CURPAGE, COMMENT_ACTIVITY);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//很重要，不然不显示就是一个坑
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }

    @Override
    public void onStop() {
        loadingUtil.stopShowLoading();
        EventbusHelper.unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case COMMENT_ACTIVITY:
                if (jsonObject != null) {
                    loadingUtil.stopShowLoading();
                    if (jsonObject.optString("error").equals("0")) {
                        Log.e("jsonObject", jsonObject.toString());
                        TextView tv_num = view.findViewById(R.id.tv_num);
                        JSONObject object = jsonObject.optJSONObject("data");
                        tv_num.setText("你已贡献" + object.optString("total") + "条评论");
                        EvaluateBean evaluateBean = GsonResolve.jsonString2Bean(jsonObject.toString(), EvaluateBean.class);
                        evaluateAdapter = new CommentAdapter(evaluateBean.getData().getItems());
                        evaluateAdapter.addHeaderView(view);
                        mRecyclerView.setAdapter(evaluateAdapter);
                        evaluateAdapter.notifyDataSetChanged();
                    }
                }
                break;
        }
    }
}
